package cn.edu.xmut.chen.zhlg.zhlg.vacancy.service.impl;

import cn.edu.xmut.chen.zhlg.common.framework.utils.AssertUtils;
import cn.edu.xmut.chen.zhlg.common.utils.ResultType;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.entity.Vacancy;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.mapper.VacancyMapper;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.param.VacancyAddParam;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.param.VacancyPageSearchOtherParam;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.param.VacancyPageSearchParam;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.result.VacancyPageResult;
import cn.edu.xmut.chen.zhlg.zhlg.vacancy.service.VacancyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;


@Service
public class VacancyServiceImpl extends ServiceImpl<VacancyMapper, Vacancy> implements VacancyService {

    @Resource
    private VacancyMapper vacancyMapper;

    @Override
    public Object create(VacancyAddParam vacancyAddParam) {
        Vacancy vacancy = new Vacancy();
        vacancy.setLevel(vacancyAddParam.getLevel())
                .setDetailed(vacancyAddParam.getDetailed())
                .setPosterId(vacancyAddParam.getPosterId())
                .setCreateTime(LocalDateTime.now())
                .setVacancyName(vacancyAddParam.getVacancyName())
                .setSalary(vacancyAddParam.getSalary())
                .setCompanyId(vacancyAddParam.getCompanyId())
                .setDeleteFlag("NOT_DELETE");
        this.save(vacancy);
        return vacancy;
    }

    @Override
    public Page<VacancyPageResult> pageSearch(VacancyPageSearchParam vacancyPageSearchParam) {
        if (vacancyPageSearchParam.getPageNum()==null){
            vacancyPageSearchParam.setPageNum(1);
        }
        if (vacancyPageSearchParam.getPageSize()==null){
            vacancyPageSearchParam.setPageSize(10);
        }
        Page<VacancyPageResult> page = new Page<>(vacancyPageSearchParam.getPageNum(), vacancyPageSearchParam.getPageSize());
        return baseMapper.pageSearch(page,
                vacancyPageSearchParam.getVacancyName(),
                vacancyPageSearchParam.getPosterId(),
                vacancyPageSearchParam.getCompanyName(),
                vacancyPageSearchParam.getAddress(),
                vacancyPageSearchParam.getIndustry(),
                vacancyPageSearchParam.getLevel(),
                vacancyPageSearchParam.getMinSalary(),
                vacancyPageSearchParam.getMaxSalary());
    }

    @Override
    public VacancyPageResult getDetail(String id) {
        AssertUtils.assertNotNull(id, ResultType.FAIL,"id不能为空");
        return baseMapper.getDetail(id);
    }

    @Override
    public Page<VacancyPageResult> getPageOther(VacancyPageSearchOtherParam vacancyPageSearchOtherParam) {
        AssertUtils.assertNotNull(vacancyPageSearchOtherParam.getId(), ResultType.FAIL,"id不能为空");
        if (vacancyPageSearchOtherParam.getPageNum()==null){
            vacancyPageSearchOtherParam.setPageNum(1);
        }
        if (vacancyPageSearchOtherParam.getPageSize()==null){
            vacancyPageSearchOtherParam.setPageSize(10);
        }
        Page<VacancyPageResult> page = new Page<>(vacancyPageSearchOtherParam.getPageNum(), vacancyPageSearchOtherParam.getPageSize());
        return baseMapper.pageSearchOther(page,vacancyPageSearchOtherParam.getId());
    }

    @Override
    public Object save(VacancyAddParam vacancyAddParam) {
        // 检查岗位是否存在
        Vacancy existingVacancy = this.getById(vacancyAddParam.getId());
        if (existingVacancy == null) {
            throw new RuntimeException("岗位不存在");
        }

        // 更新岗位信息
        Vacancy vacancy = new Vacancy();
        vacancy.setLevel(vacancyAddParam.getLevel())
                .setDetailed(vacancyAddParam.getDetailed())
                .setPosterId(vacancyAddParam.getPosterId())
                .setVacancyName(vacancyAddParam.getVacancyName())
                .setSalary(vacancyAddParam.getSalary())
                .setCompanyId(vacancyAddParam.getCompanyId())
                .setId(vacancyAddParam.getId());

        this.updateById(vacancy);
        return vacancy;
    }

}
